<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Faction' );

$query = "SELECT faction_id, score, leader, faction_type_id, control_type " .
	"FROM faction " .
	"WHERE name = '" . formatInput($_GET['name']) . "'";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
?>
					<div class="blok">
<?php
if (isset ($row['faction_id'])) {
	$score_tmp = $row['score'];
	$leader_tmp = $row['leader'];
	$faction_id_tmp = $row['faction_id'];

	$query = "SELECT count(*) " .
	"FROM faction_member " .
	"WHERE requested = 0 AND invited = 0 AND faction_id = " . $faction_id_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$members = $row['count(*)'];

	$query = "SELECT name, type_name, control_type " .
		"FROM faction " .
		"JOIN faction_types ON faction.faction_type_id = faction_types.faction_type_id " .
		"WHERE faction_id = " . $faction_id_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);

	print (translate($row['type_name']) . ": " . $row['name'] . "<br />\r\n");
	if( $row[ 'control_type' ] == 0 ) {
		$control_type_string = translate( 'Hierarchic' );
	}
	else {
		$control_type_string = translate( 'Democratic' );
	}
	print (translate('Structure') . ": " . $control_type_string . "<br />\r\n");

	print (translate("Score") . ": " . number_format($score_tmp) . "<br />\r\n");
	$query = "SELECT count(*) " .
	"FROM faction " .
	"WHERE score > " . $score_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	print translate("Rank") . ": " . number_format($row['count(*)'] + 1) . "<br />\r\n";

	$query = "SELECT name " .
	"FROM ruler " .
	"WHERE ruler_id = " . $leader_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	print (translate("Leader") . ": <a href=\"ruler.php?name=" . $row['name'] . "\">" . $row['name'] . "</a><br />\r\n");
	print (translate("Color") . ": " . get_faction_color($faction_id_tmp) . "<br />\r\n");

	print (translate("Members") . ": " . $members . "<br />\r\n");
	$query = "SELECT score FROM faction WHERE faction_id = " . $faction_id_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$average = $row['score'] / $members;
	print (translate("Average score per member") . ": " . number_format(round($average, 0)) . "<br />\r\n");

	$query = "SELECT count(*) FROM village JOIN faction_member ON faction_member.ruler_id = village.ruler_id WHERE requested = 0 AND invited = 0 AND faction_id = " . $faction_id_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	print (translate("Villages") . ": " . number_format($row['count(*)']) . "<br />\r\n");

	$query = "SELECT SUM(population) FROM village JOIN faction_member ON village.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$faction_pop = $row['SUM(population)'];
	//print( "factionpop: " . number_format( $faction_pop ) . "<br />\r\n" );
	$query2 = "SELECT SUM(scout+infantry+cavalry+artillery) FROM army JOIN faction_member ON army.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$faction_pop += $row2['SUM(scout+infantry+cavalry+artillery)'];
	//print( "factionpop: " . number_format( $faction_pop ) . "<br />\r\n" );
	$query2 = "SELECT SUM(recruits) FROM ruler JOIN faction_member ON ruler.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$faction_pop += $row2['SUM(recruits)'];
	$query2 = "SELECT SUM(population) FROM colony JOIN faction_member ON colony.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$faction_pop += $row2['SUM(population)'];
	//print( "factionpop: " . number_format( $faction_pop ) . "<br />\r\n" );

	$query2 = "SELECT SUM(population) FROM village";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$query3 = "SELECT SUM(scout+infantry+cavalry+artillery) FROM army";
	$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
	$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
	$world_pop = $row2['SUM(population)'] + $row3['SUM(scout+infantry+cavalry+artillery)'];
	$query3 = "SELECT SUM(recruits) FROM ruler";
	$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
	$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
	$world_pop += $row3['SUM(recruits)'];
	$query3 = "SELECT SUM(population) FROM colony";
	$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
	$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
	$world_pop += $row3['SUM(population)'];

	print (translate("Population") . ": " . number_format($faction_pop) . "<br />\r\n");
	$query2 = "SELECT SUM(population) FROM village";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	print (translate("Percent of world population") . ": " . round($faction_pop / $world_pop * 100, 2) . "%<br />\r\n");

	$turn = getCurrentTurn();
	$query2 = "SELECT turns_inactive FROM game";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$inactive_turn = $row2[ 'turns_inactive' ];
	
	$query2 = "SELECT SUM(score) " .
			"FROM ruler " .
			"WHERE active_turn > " . ( $turn - $inactive_turn ) . " AND resigned = 0";
	$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	$total_score = $row2['SUM(score)'];

	if( $total_score != 0 ) {
		$query2 = "SELECT SUM(score) " .
				"FROM ruler " .
				"JOIN faction_member ON faction_member.ruler_id = ruler.ruler_id " .
				"WHERE faction_id = $faction_id_tmp AND requested = 0 AND invited = 0";
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$faction_score = $row2['SUM(score)'];
		
		print (translate("Percent of total score") . ": " . round($faction_score / $total_score * 100, 2) . "%<br />\r\n");
	}
} else {
	print $_GET['name'] . ' ' . translate("doesn't exist.");
}
?>
					</div>
<?php
aos_footer();
?>